package br.com.felix.fwt.ui.image.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import br.com.felix.fwt.log.LoggerFactory;
import br.com.felix.fwt.servlet.FelixWebToolkitServlet;
import br.com.felix.fwt.servlet.FwtContext;


public class FilesystemTextImageDAO extends TextImageBuilderDAO implements Serializable{
	private static Logger logger = LoggerFactory.getLogger(FilesystemTextImageDAO.class);
	
	
	/**
     * Folder where images will be stored.
     * */
    private String imagesFolder = "/images/";

    /**
     * Folder where fonts will be stored.
     * */
    private String fontsFolder = "/fonts/";

	@Override
	public OutputStream getImageOutputStream() throws IOException {
		return new FileOutputStream(getRealFilePath(getText()));
	}
	
	
	public synchronized String getImagesFolder() {
        return imagesFolder;
    }

    public synchronized void setImagesFolder(String imagesFolder) {
        this.imagesFolder = imagesFolder;
    }
    		
	private String getFilePath(){
	    return imagesFolder + getName(getText());
	}
    /**
     * Returns the path to be used on the 'src' property of an html image.
     * */
    @Override
	public String getWebFilePath(){
	    return getFilePath();
	}
    
    /**
	 * Returns the file path on the file system.
	 * */
    private String getRealFilePath(String txt) {
    	return FelixWebToolkitServlet.getFwtContext().request.getSession().getServletContext().getRealPath(getFilePath());
    }
    
    /**
	 * Tells whether the file has already been generated with the name convention.
	 * */
	public boolean needsToBeCreated(){
        String realPath = getRealFilePath(getText());
        File file = new File(realPath);
        return !file.exists();
	}


	@Override
	public InputStream getFontInputStream() throws IOException {
		FwtContext fwtContext = FelixWebToolkitServlet.getFwtContext();
		HttpSession session = fwtContext.request.getSession();
		ServletContext servletContext = session.getServletContext();
		String realPath = servletContext.getRealPath(fontsFolder + getFont() + ".ttf");
		logger.info("Obtaining font from file '" + realPath +"'.");
        return new FileInputStream(realPath);
	}
	
	
	/**
     * Generates a camelCase name for the image.
     * */
    protected String getName(String value){
        StringBuilder name = new StringBuilder(getPrefix());
        String[] parts = value.split(" ");
        for (String part: parts){
            name.append(part.substring(0, 1).toUpperCase());
            if(part.length()>1){
                name.append(part.substring(1, part.length()).toLowerCase());
            }
        }
        name.append(".png");
        return name.toString();
    }
    
    
    /**
	 * Prefix used to name files.
	 * */
	protected String getPrefix(){
	    return "img";
	}
}